package com.project.api.sale.service;

import com.project.api.sale.dao.AttributeDao;
import com.project.api.sale.dao.PinleiDao;
import com.project.api.sale.dao.ProductDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductService {
    @Autowired
    @Qualifier("mainJdbcTemplate")
    private JdbcTemplate mainJdbcTemplate;
    public ProductDao getProductById(int id){
        String sql = "select * from product where id = ?";
        ProductDao dao = null;
        try {
            dao = (ProductDao)mainJdbcTemplate.queryForObject(
                    sql, new Object[] { id },new BeanPropertyRowMapper(ProductDao.class));
        } catch (EmptyResultDataAccessException e) {//捕获查询不到
            return new ProductDao();
        }
        return dao;
    }
    public List getProductLists(){
        String sql = "select * from product";
        List<ProductDao> productLists  = null;
        try {
            productLists = mainJdbcTemplate.query(sql,
                    new BeanPropertyRowMapper(ProductDao.class));
        } catch (EmptyResultDataAccessException e) {
            return new ArrayList();
        }
        return productLists;
    }
    public PinleiDao getProductPinlei(int pinleiId){
        String sql="select * from pinlei where id=?";
        PinleiDao dao = null;
        try {
            dao = (PinleiDao)mainJdbcTemplate.queryForObject(
                    sql, new Object[] { pinleiId },new BeanPropertyRowMapper(PinleiDao.class));
        } catch (EmptyResultDataAccessException e) {
            return new PinleiDao();
        }
        return dao;
    }
    public List<AttributeDao> getAttribute(int pinleiId){
        String sql="select * from attribute where pinleiId =? ";
        List<AttributeDao> daoLists  = null;
        try {
            daoLists = mainJdbcTemplate.query(
                    sql, new Object[] { pinleiId },new BeanPropertyRowMapper(AttributeDao.class));
        } catch (EmptyResultDataAccessException e) {
            daoLists= new ArrayList<AttributeDao>();
        }
        return daoLists;
    }
    public AttributeDao getAttributeById(int id){
        String sql="select * from attribute where id =? ";
        AttributeDao dao  = null;
        try {
            dao = (AttributeDao)mainJdbcTemplate.queryForObject(
                    sql, new Object[] { id },new BeanPropertyRowMapper(AttributeDao.class));
        } catch (EmptyResultDataAccessException e) {
            dao= new AttributeDao();
        }
        int pinleiId=dao.getPinleiId();
        PinleiDao pinleiDao=getProductPinlei(pinleiId);
        dao.setPinlei(pinleiDao);
        return dao;
    }
    /**
     * 整理品类
     * @param pinleiId
     * @return
     */
    public PinleiDao fullPinlei(int pinleiId){
         PinleiDao pinleiDao=getProductPinlei(pinleiId);
         int id=pinleiDao.getId();
//         int qty=pinleiDao.getQty();
         List<AttributeDao> lists=getAttribute(id);
         pinleiDao.setAttribute(lists);
         return pinleiDao;
    }
    /**
     * 整理product
     * @return
     */
    public ProductDao flagProduct(ProductDao productDao){
       String  pinleiIds=productDao.getPinleiId();
       String  pinleiArr[]=pinleiIds.split(",");
       List<PinleiDao> lists=new ArrayList<PinleiDao>();
       for (int i=0;i<pinleiArr.length;i++){
           String s=pinleiArr[i];
           int p=Integer.parseInt(s);
           PinleiDao dao=fullPinlei(p);
           if(dao.getId()!=0){
               lists.add(dao);
           }
       }
       productDao.setPinlei(lists);
       return productDao;
    }
}
